package com.ygqh.baby.service.impl;
import java.util.Date;
import java.util.List;

import com.ygqh.baby.ao.CodeType;
import com.ygqh.baby.ao.ContractStatus;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.SettlementStatus;
import com.ygqh.baby.po.AdminContract;
import com.ygqh.baby.po.AdminContractExample;
import  com.ygqh.baby.po.AdminSettlement;
import com.ygqh.baby.mapper.AdminContractMapper;
import  com.ygqh.baby.mapper.AdminSettlementMapper;
import com.ygqh.baby.service.AdminContractService;
import  com.ygqh.baby.service.AdminSettlementService;
import com.ygqh.baby.service.YgFrameCodeService;

import  org.springframework.util.Assert;

import java.util.Arrays;

import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;

import  com.ygqh.baby.po.AdminSettlementExample;
import  com.ygqh.baby.ao.DataStatus;

@Service
public class AdminSettlementServiceImpl implements AdminSettlementService{
	
	@Autowired
	private AdminSettlementMapper adminSettlementMapper;
	@Autowired
	private AdminContractMapper adminContractMapper;
	@Autowired
	private YgFrameCodeService ygFrameCodeService;
	
	
	@Override
	public List<AdminSettlement> find() {
		AdminSettlementExample example = new AdminSettlementExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminSettlementMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminSettlementExample example = new AdminSettlementExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminSettlementMapper.countByExample(example);
	}

	@Override
	public AdminSettlement findById(Long id) {
		return adminSettlementMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(AdminSettlement adminSettlement) {
		String code="JS"+ygFrameCodeService.getFrameCode(CodeType.Settlement,new Date(), 2);
		adminSettlement.setSettlementCode(code);
		//判断合同状态是否是结算中，不是的话更新为结算中 
		AdminContractExample example=new AdminContractExample();
		example.createCriteria().andContractCodeEqualTo(adminSettlement.getContractCode());
 		List<AdminContract> contract=adminContractMapper.selectByExample(example);
		if(contract.get(0).getContractStatus().equals(ContractStatus.Archived)){
			AdminContract c=new AdminContract();
			c.setContractStatus(ContractStatus.Buying);
			c.setId(contract.get(0).getId());
			adminContractMapper.updateByPrimaryKeySelective(c);
		}
		return adminSettlementMapper.insertSelective(adminSettlement);
	}

	@Override
	public int update(AdminSettlement adminSettlement) {
		return adminSettlementMapper.updateByPrimaryKeySelective(adminSettlement);
	}

	@Override
	public ResultSet<AdminSettlement> search(QueryInfo queryInfo,String q,Long id,String supplierName,String contractCode,Date startDate,Date endDate,SettlementStatus status) {
		List<AdminSettlement>  list =  adminSettlementMapper.selectSuper(queryInfo, q,id,supplierName,contractCode,startDate,endDate,status==null?null:status.name());
		int count = adminSettlementMapper.countSuper(q,id,supplierName,contractCode,startDate,endDate,status==null?null:status.name());
		ResultSet<AdminSettlement> resultSet = new ResultSet<AdminSettlement>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminSettlementExample example = new AdminSettlementExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminSettlement record = new AdminSettlement();
		record.setStatus(DataStatus.Delete);

		return adminSettlementMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(AdminSettlement adminSettlement) {
		int r = 0;
		if(adminSettlement.getId() != null) {
			r = this.update(adminSettlement);
		}else {
			r = this.save(adminSettlement);
		}
		return r;
	}
	@Override
	public AdminSettlement findSettlementById(Long id){
	  List<AdminSettlement>  list =  adminSettlementMapper.selectSuper(null, null,id,null,null,null,null,null);
	  if(list!=null &&list.size()>0)
		  return list.get(0);
	  return null;
	
	
	}
	@Override
	public int findByPurchaseCode(String purchaseCode){
		AdminSettlementExample example = new AdminSettlementExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andPurchaseCodeEqualTo(purchaseCode);
		return adminSettlementMapper.countByExample(example);
	}
}

